package com.neuedu.springmvc.controller;

import com.neuedu.springmvc.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 金山
 * 项目：springmvc-learn
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/1714:35
 */
@Controller
public class ParamsController {


    /**
     *
     * http://localhost:8080/web/p1
     * request\response
     * @return
     */
    @RequestMapping("/p1")
    @ResponseBody
    public void p1(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //可以使用request

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h1>使用原生的Response响应的数据</h1>");
    }

    @RequestMapping("/p2")
    @ResponseBody
    public void p2(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {

        //可以使用request
        String name = request.getParameter("name");
        System.out.println("name = " + name);

        ServletContext application = request.getServletContext();
        // ServletContext application = session.getServletContext();

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("{\"name\":\"张三\",\"age\":18}");
    }


    /**
     * http://localhost:8080/web/p3?name=zhangsan&age=18&address=HLJ
        在方法的形参中直接 添加  参数名称同名的  参数
        RequestParam 可以将 请求中的不同名参数绑定到 方法的参数列表中   address=HLJ   --》 myaddress
     RequestParam  是否必须：  必须的
                    required : 默认 true   如果非必须 可以为 false
     * @return
     * @throws IOException
     */
    @RequestMapping("/p3")
    @ResponseBody
    public String p3(String name,int age, @RequestParam(value = "address",required = false) String myaddress) throws IOException {


        StringBuffer params = new StringBuffer();
        params.append("name="+name);


        params.append("&");
        params.append("age="+age);
        params.append("&");
        params.append("myaddress="+myaddress);


        return params.toString();
    }



    @RequestMapping("/p4")
    @ResponseBody
    public String p4(String username ,String password ,int age,String expireDate) throws IOException {


        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(age);


        // /user 将 user信息存储到 数据库中

        return user.toString();
    }


    /**
     * 使用简单对象接受参数
     * @param user
     * @return
     * @throws IOException
     */

    @RequestMapping("/p5")
    @ResponseBody
    public String p5(User user) throws IOException {

        System.out.println(user);

        // /user 将 user信息存储到 数据库中

        return user.toString();
    }


    @RequestMapping("/p6")
    @ResponseBody
    public String p6(@RequestBody  User user) throws IOException {

        System.out.println(user);

        // /user 将 user信息存储到 数据库中

        return user.toString();
    }


}
